<?php

session_start();
require('../FPDF/fpdf.php');
include("../includes/conexion.php");

class PDF extends FPDF {

//Page header
    function Header() {
        $this->SetFont('Arial', 'B', 14);
        //Move to the right
        $this->Cell(1);
        //Title
        $this->Cell(188, 15, 'UNIVERSIDAD NACIONAL  AGRARIA DE LA SELVA', 0, 0, 'C');
        //Line break
        $this->Ln(14);

        $this->SetFont('Arial', 'B', 13);
        //Move to the right
        $this->Cell(1);
        //Title
        $this->Cell(188, 13, 'Reporte de Existencia de Documentos', 0, 0, 'C');
        //Line break
        $this->Ln(13);
        $this->SetFont('Arial', 'B', 10);
        //Move to the right
        $this->Cell(1);
        //Title
        $fechas = date("d/m/Y");
        $this->Cell(15, 10, 'Fecha:', 0, 0, 'L') + $this->Cell(20, 10, $fechas, 0, 0, 'R');
        //Line break
        $this->Ln(10);
    }

//Page footer
    function Footer() {
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        //Page number
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
    }

    function LoadData($tipo) {
        global $handle;



        $sql_detalle = "select d.id_doc,
d.numero_ingreso,
d.titulo_LTRP, (select count(*) from existencias_documento	 where id_doc = d.id_doc )as existencias 
				from documento d	  
	  			where id_tipo_documento='$tipo'";

        $qry_detalle = mysql_query($sql_detalle);
        $data = array();
        while ($lines = mysql_fetch_array($qry_detalle))
            $data[] = $lines;
        return $data;
    }

    function ImprovedTable($header, $data) {
        //Column widths
        $w = array(6, 16, 120, 14);
        $this->Cell(1);
        //Header
        for ($i = 0; $i < count($header); $i++)
            $this->Cell($w[$i] + 8, 7, $header[$i], 1, 0, 'C');
        $this->Ln();
        //Data
        $this->Cell(1);
        foreach ($data as $row) {

            $this->Cell($w[0] + 8, 6, $row[0], 'LR', 0, 'C');
            $this->Cell($w[1] + 8, 6, $row[1], 'LR', 0, 'C');
            $this->Cell($w[2] + 8, 6, $row[2], 'LR', 0, 'L');
            $this->Cell($w[3] + 8, 6, $row[3], 'LR', 0, 'C');
            $this->Ln();
            $this->Cell(1);
            $toting = $toting + $row[1];
            $totales = $totales + $row[3];
            ;
        }
        //Closure line
        $this->Cell(188, 0, '', 'T');
        //$this->Cell(array_sum($w),0,'','T');
        $this->Ln();
        $this->Cell(1);
        //Title
        $this->Cell(14, 8, 'TOTALES.', 0, 0, 'L') + $this->Cell(24, 8, $toting, 0, 0, 'C');
        +$this->Cell(128, 8, '', 0, 0, 'C');
        +$this->Cell(22, 8, $totales, 0, 0, 'C');
    }

}

$pdf = new PDF();
$pdf->AliasNbPages();
//Column titles
$header = array('Codigo', 'Num. Ingreso', 'Titulo', 'Existencias');
//Data loading
$data = $pdf->LoadData('1');
$pdf->SetFont('Arial', '', 8);
$pdf->AddPage();
$pdf->ImprovedTable($header, $data);
$pdf->Output("reportes_existencias.pdf", "F");
?>
